package com.app.twth.ui.checkin.submit;


import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityCheckinSubmitBinding;
import com.app.twth.ui.checkin.Container;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;

public class CheckinSubmitActivity extends BaseActivity<ActivityCheckinSubmitBinding, CheckinSubmitViewModel> {
    private String TAG = getClass().getSimpleName();
    int index = 0;
    private Container container;

    @Override
    protected void initListener() {
        viewModel.infoResultCode.observe(this, code -> {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "Checkin(Submit)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setResetGone();
        setNext(getString(R.string.submit));
        container = (Container) getIntent().getBundleExtra(AppRouter.K_EXTRA).getSerializable("Container");

        viewModel.containerNo.setValue(container.getContainerNo() + "");
        if (container.getCartonQty() > 0) {
            viewModel.cartonsQty.setValue(container.getCartonQty() + "");
        }
        if (container.getPalletQty() > 0) {
            viewModel.palletQty.setValue(container.getPalletQty() + "");
        }
        if (container.getOperationMethod() > 0) {
            index = container.getOperationMethod() - 1;
        } else {
            index = container.getOperationMethod();
        }
        if (container.getPallet() == 1) {
            switch (index) {
                case 0:
                    viewModel.operationMethod.setValue("container");
                    binding.layoutQty.setVisibility(View.VISIBLE);
                    binding.layoutCartonsQty.setVisibility(View.GONE);
                    break;
                case 1:
                    viewModel.operationMethod.setValue("truck");
                    binding.layoutQty.setVisibility(View.VISIBLE);
                    binding.layoutCartonsQty.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    viewModel.operationMethod.setValue("express");
                    binding.layoutQty.setVisibility(View.VISIBLE);
                    binding.layoutCartonsQty.setVisibility(View.VISIBLE);
                    break;
            }
        } else {
            switch (index) {
                case 0:
                    viewModel.operationMethod.setValue("container");
                    binding.layoutQty.setVisibility(View.GONE);
                    binding.layoutCartonsQty.setVisibility(View.GONE);
                    break;
                case 1:
                    viewModel.operationMethod.setValue("truck");
                    binding.layoutQty.setVisibility(View.GONE);
                    binding.layoutCartonsQty.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    viewModel.operationMethod.setValue("express");
                    binding.layoutQty.setVisibility(View.GONE);
                    binding.layoutCartonsQty.setVisibility(View.VISIBLE);
                    break;
            }
        }

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_next:
                if (System.currentTimeMillis() - timeMillis < spaceTime) {//两秒内触发一次
                    return;
                }
                timeMillis = System.currentTimeMillis();
                submitData();
                break;
        }
    }


    @Override
    public void getScanninCode() {
        super.getScanninCode();
    }


    private void submitData() {
        CheckinSubmitQr receivingQr = new CheckinSubmitQr();
        receivingQr.setId(container.getId());

        if (container.getPallet() == 1) {
            if (Utils.isEmpty(viewModel.palletQty.getValue()) || (Integer.parseInt(viewModel.palletQty.getValue()) == 0)) {
                ToastUtil.showToast("Please enter the number of pallets");
                return;
            }
            if (index > 0) {
                if (Utils.isEmpty(viewModel.cartonsQty.getValue()) || (Integer.parseInt(viewModel.cartonsQty.getValue()) == 0)) {
                    ToastUtil.showToast("Please enter the number of cartons");
                    return;
                }
            }
        } else {
            Log.e(TAG, "index:" + index);
            switch (index) {
                case 0:
//                    if (Utils.isEmpty(palletQty) || (Integer.parseInt(palletQty) == 0)) {
//                        ToastUtil.showToast("Please enter the number of pallets");
//                        return;
//                    }
                    break;
                case 1:
                case 2:
                    if (Utils.isEmpty(viewModel.cartonsQty.getValue()) || (Integer.parseInt(viewModel.cartonsQty.getValue()) == 0)) {
                        ToastUtil.showToast("Please enter the number of cartons");
                        return;
                    }
                    break;
            }
        }

        if (!Utils.isEmpty(viewModel.palletQty.getValue())) {
            receivingQr.setPalletQty(Integer.parseInt(viewModel.palletQty.getValue()));
        }
        if (!Utils.isEmpty(viewModel.cartonsQty.getValue())) {
            receivingQr.setCartonsQty(Integer.parseInt(viewModel.cartonsQty.getValue()));
        }
        viewModel.checkinSubmit(receivingQr);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
